<template>
  <view class="wrap">
     <top-tab tabTitle="我的钱包" @backClick="backClick" backColor="#5B2177"/>
		 <view class="money-back-box">
			<view class="backg-money">
				<view class="title-money">可提现金额</view>
				<view class="money">￥{{balance}} CNY</view>
			</view>
		 </view>
		 <!-- 选项卡 -->
		 <view class="tabs-box">
		 	<ms-tabs :list="list" v-model="active" itemColor='#A912E7' lineColor='#A912E7'/>
		 </view>
		 <!-- 列表 -->
		 <scroll-view class="main-scroller"  refresher-enabled="true" :refresher-triggered="triggered" :refresher-threshold="100" :style="{height:navHeight+'px'}" @refresherrefresh="onRefresh"
			@refresherabort="onAbort" @scrolltolower="scrollBottom" scroll-y="true" scroll-with-animation="true">
				<view class="main" v-if="moneyList.length">
					 <view class="record-list" v-for="(item,index) in moneyList" :key="item.id">
						<view class="title-num">
							<view class="title">申请提现</view>
							<view class="money">+{{item.amount}}</view>
						</view>
						<view class="hint">{{item.text}}</view>
						<view class="time-failed">
							<view class="time">{{item.createTime}}</view>
							<view class="failed" :style="{color: item.color}">{{item.stateName}}</view>
						</view>
					 </view>
					 	<uni-load-more style="padding: 20rpx;"  :status="loadingType" iconType="auto"></uni-load-more>
				</view>
				<no-data v-else></no-data>
			</scroll-view>
		 <view class="footer">
			 <!-- <view class="btn-depo">提现</view> -->
			 <view class="btn-topup" @click="drawMoney">提现</view>
		 </view>
  </view>
</template>

<script>
	import topTab from '@/components/top-tabbar.vue'
	import msTabs from '@/components/ms-tabs.vue'
	import { $POST, $GET } from '@/network/api.js'
	import { formatDate } from '@/network/date.js'
	import noData from '@/components/nodata.vue'
  export default {
		components: { topTab, msTabs, noData },
    data() {
      return {
				buyFrom: { // 交易记录参数
					pageNum: 1,
					pageSize: 10,
					state: 1 , // 订单状态：支付中=0，1=成功，2=取消购买，3=失败（拒绝和处理失败），4=待审核
				},
				triggered: false, // 下拉刷新开关
				videoList: [], // 用于分页长度
				loadingType: 'loading', // 加载更多状态
				balance: '', // 余额
				navHeight: 0, // 主体内容高度
				windoHeight: 0, // 可见高度
				active: 0,
				list:[{ title:'提现记录', value: 1 }],
				moneyList: [] // 列表数据
			}
    },
		onReady(){
			this.getHight() // 获取高度
		},
		onLoad() {
			this.getBalance()
			this.getBuy()
		},
		methods: {
			// 申请提现
			drawMoney() {
				uni.navigateTo({
					url:'/pages/my/agency/draw_money?balance='+this.balance
				})
			},
			// 关闭下拉刷新
			onAbort() {
				const vm = this
				setTimeout(() => {
					vm.triggered = false;
				}, 100)
			},
			// 下拉刷新
			onRefresh() {
				const vm = this
				vm.triggered = true	
				vm.getBuy() 		
			 },
			 // 获取购买记录列表
			 async getBuy() {
			 		const	res = await $GET("/boke-pay/withdraw/queryList",this.buyFrom)
			 			this.onAbort()
			 			this.videoList = JSON.parse(JSON.stringify(res.data))
			 			this.videoList.forEach(item => {
			 				item.createTime = formatDate(item.createTime)
			 				item.updateTime = formatDate(item.updateTime || 0)
			 				item.goodsName ="提现金额"
			 				if(item.state === 0) {
			 					item.stateName = "支付中"
			 					item.color = "#f69045"
			 				}
			 				if(item.state === 1) {
			 					item.stateName = "成功"
			 					item.color = "#10B86B"
			 				}
			 				if(item.state === 2) {
			 					item.stateName = "取消"
			 					item.color ="#999999"
			 				}
			 				if(item.state === 3) {
			 					item.stateName = "失败"
			 					item.color ="#F11616"
			 				}
			 				if(item.state === 4) {
			 					item.stateName = "待审核"
			 					item.color ="#4599F6"
			 				}
			 			})
			 			this.moneyList = [...this.moneyList,... this.videoList]
			 },
			// 滚动到底部
			scrollBottom() {
				this.buyFrom.pageNum = this.buyFrom.pageNum +1
				if(this.videoList.length < 10) {
					this.loadingType='nomore'
					return
				}
				this.getBuy() 
			},
			// 提现余额
			async getBalance() {
				const res = await $GET("/boke-user/user/wallet")
				this.balance = res.data.balance
			},
		getHight() {
			const vm = this;
			uni.getSystemInfo({ //调用uni-app接口获取屏幕高度
				success(res) { //成功回调函数
					vm.windoHeight = res.windowHeight //windoHeight为窗口高度，主要使用的是这个
					let titleH = uni.createSelectorQuery().select(".main-scroller")
					titleH.boundingClientRect(data=>{
						vm.navHeight = vm.windoHeight - data.top - 85 //计算高度：元素高度=窗口高度-元素距离顶部的距离（data.top）
					}).exec()
				}
			})
		},
		// 返回上一页
		backClick() {
			uni.navigateTo({
			    url: '/pages/my/agency/agency'
			});
		}
	}
}
</script>

<style lang="scss" scoped>
	.money-back-box{
		padding: 37rpx;
		.backg-money{
			width: 100%;
			height: 270rpx;
			background: linear-gradient(45deg, #FF5023 0%, #C758EC 98%);
			box-shadow: 0px 8rpx 16rpx 0px rgba(124, 133, 236, 0.28);
			border-radius: 20rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			padding-left: 40rpx;
			box-sizing: border-box;
			// align-items: center;
			.title-money{
				font-size: 30rpx;
				font-family: PingFang SC;
				color: #FFFFFF;
			}
			.money{
				margin-top: 10rpx;
				font-size: 50rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
			}
		}
	}
	.tabs-box{
		height: 100rpx;
		border-bottom: 1px solid #F2F2F2;
		line-height: 100rpx;
		/deep/ .tab{
			height: 96rpx !important;
			line-height: 96rpx !important;
		}
		/deep/ .tab__line {
			height: 6rpx !important;
		}
		/deep/ .tab__item {
			line-height: 96rpx !important;
		}
		/deep/ .tab__item-title {
			margin: 0 35rpx !important;
		}
	}
	.main{
		padding: 40rpx 30rpx;
		.record-list{
			width: 100%;
			background: #FFFFFF;
			box-shadow: 0px 8px 16px 0px rgba(124, 133, 236, 0.28);
			padding: 34rpx 20rpx 43rpx 30rpx;
			box-sizing: border-box;
			margin-bottom: 20rpx;
			.title-num{
				display: flex;
				justify-content: space-between;
				align-items: center;
				.title{
					font-size: 30rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #333333;
				}
				.money{
					font-size: 40rpx;
					font-family: Arial;
					font-weight: 400;
					color: #FE3035;
				}
			}
			.hint{
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #999999;
				margin: 34rpx 0 28rpx 0;
			}
			.time-failed{
				display: flex;
				align-items: center;
				justify-content: space-between;
				.time{
					font-size: 20rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #999999;
				}
				.failed{
					font-size: 26rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #ED2E2E;
				}
			}
		}
	}
	.footer{
		width: 100%;
		height: 140rpx;
		position:fixed;
		left: 0;
		bottom: 0;
		background: #FFFFFF;
		box-shadow: -8px -1px 13px 0px rgba(0, 0, 0, 0.08);
		display: flex;
		align-items: center;
		justify-content: center;
		margin-bottom:  env(safe-area-inset-bottom);
		font-size: 30rpx;
		color: #333333;
		.btn-depo{
			width: 220rpx;
			height: 80rpx;
			background: #E9E9E9;
			border-radius: 40rpx;
			line-height: 40px;
			text-align: center;
		}
		.btn-topup{
			width: 220rpx;
			height: 80rpx;
			background: linear-gradient(90deg, #B83DF1, #852CAE);
			border-radius: 40rpx;
			line-height: 40px;
			text-align: center;
			color: #FEFEFE;
			margin-left: 40rpx;
		}
	}
</style>
